from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time

# browser = webdriver.Chrome()
# browser.get('https://www.jd.com')
# browser.get('https://www.baidu.com')
# browser.get('https://www.taobao.com')
#
# """后退"""
# browser.back()  # 百度
# time.sleep(3)
# """前进"""
# browser.forward()  # 淘宝
# time.sleep(3)


# """新增窗口"""
# browser = webdriver.Chrome()
# browser.get('https://www.baidu.com')
# time.sleep(2)
#
# browser.execute_script('window.open()')  # 打开新窗口
# browser.switch_to.window(browser.window_handles[1])  # 切到新窗口
# browser.get('https://www.jd.com')
# time.sleep(2)
# browser.execute_script('window.open()')
# browser.switch_to.window(browser.window_handles[2])
# browser.get('https://www.taobao.com')


"""异常处理"""

browser = webdriver.Chrome()
try:
    browser.get('https://www.baidu.com')
except TimeoutException:
    print('Time out')
try:
    browser.find_element(By.ID, 'hello')
except NoSuchElementException:
    print('No Element')
finally:  # 无论try语句中是否抛出异常，finally中的语句一定会被执行
    browser.close()
